<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">

    <style type="text/css">
        tbody .layui-table-cell{
            text-align:center;
            height: 150px;
            line-height: 150px;
            overflow: hidden;
        }
        .layui-table img{
            max-width:100px;
        }
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" method="post" action="${path}/movies/all">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">电影名</label>
                            <div class="layui-input-inline">
                                <input type="text" name="name" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">演员</label>
                            <div class="layui-input-inline">
                                <input type="text" name="actor" autocomplete="off" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit
                                    lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加电影</button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete">批量下架</button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="show">批量上映</button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>


        <div id="test"></div>
        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete"></a>
        </script>

    </div>

</div>
<script src="../../lib/jquery-3.4.1/jquery-3.4.1.min.js"></script>
<script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="../../js/lay-module/tableSelect/tableSelect.js"></script>
<script type="text/html" id="zizeng">
    {{d.LAY_TABLE_INDEX+1}}
</script>
<script>
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;

        table.render({
            elem: '#currentTableId',
            url: '${path}/movies/all',
            height: 685,
            cellMinWidth: 80,
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '电影列表',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox", width: 50},
                {field:'tid', width:60, title: '序号',templet:'#zizeng'},
                {field: 'id', width: 80, title: '电影ID', align: "center"},
                {field: 'name', width: 150, title: '电影名', align: "center"},
                {field: 'movieImg', width: 200, title: '电影海报', align: "center",templet:'#imgTemp'},
                {field: 'typeName', width: 150, title: '电影类型', align: "center"},
                {field: 'actor', width: 160, title: '导演', align: "center"},
                {field: 'detail', width: 150, title: '电影描述', align: "center"},
                {field: 'duration', width: 120, title: '时长', align: "center"},
                {field: 'price', width: 120, title: '电影单价', align: "center"},
                {field: 'score', width: 120, title: '评分', align: "center"},
                {field: 'commentnum', width: 120, title: '票房', align: "center"},
                {field: 'state', width: 120, title: '电影状态', align: "center",sort:true},
                {field:'opera',title: '操作', minWidth: 120, toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 10,
            page: true,
            skin: 'line',
            done:function (){
                $("[data-field='state']").children().each(function(){
                    if($(this).text()=='1'){
                        $(this).text("上映")
                        $(this).parent().next().find(".data-count-delete").eq(0).text("下架")
                    }else if($(this).text()=='0'){
                        $(this).text("下架")
                        $(this).parent().next().find(".data-count-delete").eq(0).text("上映")
                    }
                });

            }
        });


        //监听性别操作
        form.on('switch(sexDemo)', function (obj) {
            layer.tips(this.value + ' ' + this.name + '：' + obj.elem.checked, obj.othis);
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            let result = JSON.stringify(data.field);
            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: {
                    searchParams: result
                }
            }, 'data');
            //执行搜索重载
            return false;
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '添加电影',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['700px', '600px'],
                    content: 'add.jsp',
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data, delList=[];
                data.forEach(function(n,i){
                    delList.push(n.id);
                });
                if (delList.length === 0){
                    layer.msg("请选择其中的一行!");
                }else {
                    layer.confirm('确认下架这些电影吗', function (index) {
                        $.post('${path}/movies/delMovie',{"ids":delList},function (result){
                            if(result.code=="200"){
                                layer.msg('下架成功！', {icon:1});
                                table.reload("currentTableId",{
                                    page:{
                                        curr: $(".layui-laypage-em").next().html()
                                    }
                                });
                            }else {
                                layer.msg('下架失败！', {icon:2});
                            }
                        })
                    });

                    console.log(delList)
                }
            }else if (obj.event === 'show') {  // 监听上映操作
                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data, showList=[];
                data.forEach(function(n,i){
                    showList.push(n.id);
                });
                if (showList.length === 0){
                    layer.msg("请选择其中的一行!");
                }else {
                    layer.confirm('确认上映这些电影吗', function (index) {
                        $.post('${path}/movies/showMovie',{"ids":showList},function (result){
                            if(result.code=="200"){
                                layer.msg('上映成功！', {icon:1});
                                table.reload("currentTableId",{
                                    page:{
                                        curr: $(".layui-laypage-em").next().html()
                                    }
                                });
                            }else {
                                layer.msg('上映失败！', {icon:2});
                            }
                        })
                    });
                }
                // layer.alert(JSON.stringify(data));
            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            let data = obj.data;
            console.log(data)
            if (obj.event === 'edit') {
                let index = layer.open({
                    title: '编辑电影信息',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: 'edit.jsp',
                    success:function(layero, index) {
                        let iframe = window['layui-layer-iframe' + index];
                        iframe.child(data);
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                })
                return false;
            } else if (obj.event === 'delete') {
                let state = obj.tr.children().eq(13).find(".data-count-delete").text()
                let path ;
                layer.confirm('确认'+state+'该电影吗', function (index) {
                    if(state=="上映"){
                        path = 'showMovie'
                    }else {
                        path = 'delMovie'
                    }
                    let list = [];
                    list.push(data.id);
                    $.post('${path}/movies/'+path,{"ids":list},function (result){
                        if(result.code=="200"){
                            layer.msg(state+'成功！', {icon:1});
                            table.reload("currentTableId",{
                                page:{
                                    curr: $(".layui-laypage-em").next().html()
                                }
                            });
                        }else {
                            layer.msg(state+'失败！', {icon:2});
                        }
                    })

                    layer.close(index);
                });
            }
            });


        //监听单元格编辑
        table.on('edit(currentTableFilter)', function(obj){
            var value = obj.value //得到修改后的值
                ,data = obj.data //得到所在行所有键值
                ,field = obj.field; //得到字段
            layer.msg('[ID: '+ data.id +'] ' + field + ' 字段更改为：'+ value);
        });
    });


</script>
<script type="text/html" id="imgTemp">
    <div><img id="image" src="http://localhost:80/movies/{{d.movieImg}}"/></div>
</script>
</body>
</html>